//ToDo : create head
$(document).ready(function () {
	//element selected generic
	$('#ihm-content-listing li.navigator').live('click', function (e) {
		$element = $(this);
		if ($(this).hasClass('ajax')) {
			$parent = $(this).parents('ul');
			$.ajax({
			   url: $(this).attr('url'),
			   beforeSend: function () {
					wait_block($parent.attr('id'), JAVASCRIPT_WAIT);
					$('#ihm-view-search').remove();
			   },
			   success: function (message, status) {
				   if ($parent.attr('id')) {
				    	if (message != $('#' + $parent.attr('id')).html()) {
				    		$('#' + $parent.attr('id')).html(message);
				    		if (!$element.hasClass('abc-element')) {
				    			$('#' + $parent.attr('id')).attr('old-query', $element.attr('url'));
				    		}
				    	}
				   }
			   },
			   complete: function () {
				   if ($element.hasClass('navigator')) {
					   // add element to pathfinder
					   pathfinder($element);
				   }
				   $("#ihm-view").animate({scrollTop: 0}, 'slow');
				   $('.ui-slider-handle').animate({bottom: '100%'}, 'slow');
				   setTimeout(function () {
					   //callback person
					   callback();
					   $("#ihm-scrollbar").css('height', $(".ihm-listing").height()-($('#ihm-pathfinder').height() + 15));
					   setTimeout(function() {
						   _scrollbar();
						   $("#ihm-content .content").css({top:0});
						   var stop = false;
					   }, 500);
				   }, 400);
				  
		    	}
			});
		}
		return false;
	});
	
	//person view selection
	$('#ihm-content-listing li.person').live('click', function (e) {
		$element = $(this);
		if ($(this).hasClass('person')) {
			$parent = $(this).parents('ul');
			if (!$(this).hasClass('selected')) {
				$.ajax({
				   url: $(this).attr('url'),
				   beforeSend: function () {
						$('#ihm-view .generic').remove();
						wait_block('ihm-view', 'WAIT...');
						$('#ihm-view-search').remove();
				    	   
				   },
				   success: function (message, status) {
					   $('#ihm-view-person').html(message);
					   $('#ihm-view-person').attr('old-query', $element.attr('url'));
					   $('#ihm-view-person').fadeOut(50).fadeIn(400);
				   },
				   complete: function () {
					   $('#ihm-content-listing li.person').removeClass('selected');
					   $element.addClass('selected');
					   $("#ihm-view").animate({scrollTop: 0}, 'slow');
					   $('.ui-slider-handle').animate({bottom: '100%'}, 'slow');
					   setTimeout(function () {
						   //callback person
						   callback();
					   }, 400);
				   },
				   error: function() {
					   $('.path-element').eq($('.path-element').length-2).click();
				   }
				});
			}
			else {
				$('#ihm-content-listing li.selected').removeClass('selected');
				access_path($('.path-element').last());
			}
			
		}
		return false;
	});
	
	//element selected menu left
	$('.ihm-menu li').live('click', function (e) {
		$element = $(this);
		if ($(this).hasClass('ajax')) {
			$parent = $('#ihm-content-listing');
			$.ajax({
			   url: $(this).attr('url'),
			   beforeSend: function () {
					wait_block($parent.attr('id'), JAVASCRIPT_WAIT);
					$('#ihm-view .generic').remove();
					$('#ihm-view .element').hide();
					wait_block('ihm-view', 'WAIT...');
					$('#ihm-view-search').remove();
			   },
			   success: function (message, status) {
				   if ($parent.attr('id')) {
				    	if (message != $('#' + $parent.attr('id')).html()) {
				    		$('#' + $parent.attr('id')).html(message);
				    		if (!$element.hasClass('abc-element')) {
				    			$('#' + $parent.attr('id')).attr('old-query', $element.attr('url'));
				    		}
				    	}
				   }
			   },
			   complete: function () {
				   
				   if ($element.hasClass('navigator')) {
					   // add element to pathfinder
					   pathfinder($element);
				   }
				   
				   //add class selected
				   $element.parent().find('li').each(function() {
					   $(this).removeClass($(this).attr('id') + '-selected');
				   });
				   $element.addClass($element.attr('id') + '-selected').addClass('selected');
			   }
			});
			
		}
		return false;
	});
	
	// span all click
	$('span.path-element-counter').live('click', function (e) {
		$em = $(this).find('em').eq(0);
		if ($em.html() != 0) {
			//launch query for persons
			$oldquery = $em.attr('old-query').replace('/counter/1', '');
			if ($oldquery) {
				$.ajax({
				   url: $oldquery,
				   beforeSend: function () {
						wait_block('ihm-content-listing', JAVASCRIPT_WAIT);
						$('#ihm-view-search').remove();
						$em.parent().parent().removeClass( $em.parent().parent().attr('id') + '-selected' );
				   },
				   success: function (message, status) {
					   $('#ihm-content-listing').html(message);
					   $('#ihm-content-listing').attr('old-query', $oldquery);
					   page = 2;
					  $em.parent().parent().find('ul').eq(0).fadeOut();
					  $('#ihm-view .element').hide();
				   },
				   complete: function() {
					   setTimeout(function() {
						   _scrollbar();
						   $("#ihm-content .content").css({top:0});
						   var stop = false;
					   }, 800);
			       }
				});
			}
		}
		
		//break parent click
		return false;
	});
	
	// buttons details
	$('a.details').live('click', function (e) {
	   text = $(this).html();
	   rel  = $(this).attr('rel'); 
	   if ($(this).hasClass('open')) {
		   $(this).parent().find('p.hidden').fadeOut();
		   $(this).removeClass('open');
	   }
	   else {
		   $(this).parent().find('p.hidden').fadeIn();
		   $(this).addClass('open');
	   }
	   $(this).html(rel);
	   $(this).attr('rel', text);
	   return false;
	});
});